package controller

import (
	"advertising/config/mysql"
	"advertising/internal/app/web/service"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Material struct{}

// Save 素材上传
func (Material) Save(c *gin.Context) {
	var params struct {
		Name     string `json:"name"`
		FilePath string `json:"file_path"`
		Position int8   `json:"position"`
		Type     int8   `json:"type"`
		FolderID string `json:"folder_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	merchantName := c.GetString("merchant_name")
	userID := c.GetString("user_id")
	username := c.GetString("username")
	db := mysql.NewDB()
	id, newFilePath, err := service.Material{}.Save(c.Request.Context(), db, merchantID, merchantName, userID,
		username, params.Name, params.FilePath, params.Position, params.Type, params.FolderID)
	response.JSON(c, gin.H{
		"id":       id,
		"filepath": newFilePath,
	}, err)
}

// List 素材列表
func (Material) List(c *gin.Context) {
	var params struct {
		Type     int8   `json:"type"`
		Name     string `json:"name"`
		Position int    `json:"position"`
		Status   int8   `json:"status"`
		Page     int    `json:"page"`
		Limit    int    `json:"limit"`
		FolderID string `json:"folder_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	list, count, err := service.Material{}.List(c.Request.Context(), db, merchantID, params.Type, params.Name, params.Position, params.Status, params.Page, params.Limit, params.FolderID)
	response.JSON(c, gin.H{
		"count": count,
		"list":  list,
	}, err)
}

// Delete 素材删除
func (Material) Delete(c *gin.Context) {
	var params struct {
		IDs string `json:"ids"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)

	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	err := service.Material{}.Delete(c.Request.Context(), db, merchantID, params.IDs)
	response.JSON(c, nil, err)
}

// MaterialInUseList 可用素材列表
func (Material) MaterialInUseList(c *gin.Context) {
	var params struct {
		Type     int8   `json:"type"`
		Name     string `json:"name"`
		Position int    `json:"position"`
		Page     int    `json:"page"`
		Limit    int    `json:"limit"`
		FolderID string `json:"folder_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	list, count, err := service.Material{}.MaterialInUseList(c.Request.Context(), db, merchantID, params.Type, params.Name, params.Position, params.Page, params.Limit, params.FolderID)
	response.JSON(c, gin.H{
		"count": count,
		"list":  list,
	}, err)
}

// AuditingMaterialCount 审核中素材统计
func (Material) AuditingMaterialCount(c *gin.Context) {
	merchantID := c.GetString("merchant_id")
	count := service.Material{}.AuditingMaterialCount(c.Request.Context(), merchantID)
	response.JSON(c, gin.H{
		"count": count,
	}, nil)
}

// 素材移动文件夹
func (Material) MoveFolder(c *gin.Context) {
	var params struct {
		IDs      string `json:"ids"`
		FolderID string `json:"folder_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	err := service.Material{}.MoveFolder(c.Request.Context(), merchantID, params.IDs, params.FolderID)
	response.JSON(c, nil, err)
}

// FolderSave 分组新增编辑
func (Material) FolderSave(c *gin.Context) {
	var params struct {
		ID   string `json:"id"`
		PID  string `json:"pid"`
		Type int8   `json:"type"`
		Name string `json:"name"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	err := service.MaterialFolder{}.FolderSave(c.Request.Context(), merchantID, params.ID, params.PID, params.Type, params.Name)
	response.JSON(c, nil, err)
}

// FolderList 分组列表
func (Material) FolderList(c *gin.Context) {
	var params struct {
		Type int8 `json:"type"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	list, err := service.MaterialFolder{}.FolderList(c.Request.Context(), merchantID, params.Type)
	response.JSON(c, list, err)
}

// FolderDelete 分组删除
func (Material) FolderDelete(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	err := service.MaterialFolder{}.FolderDelete(c.Request.Context(), merchantID, params.ID)
	response.JSON(c, nil, err)
}

// FolderSort 分组排序
func (Material) FolderSort(c *gin.Context) {
	var params struct {
		Sort []struct {
			ID   string `json:"id"`
			Sort int8   `json:"sort"`
		} `json:"sort"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	err := service.MaterialFolder{}.FolderSort(c.Request.Context(), merchantID, params.Sort)
	response.JSON(c, nil, err)
}
